#include "header.h"

static int 		myid, np, flag, n, buffsize;
static int		i, j, k;
static char 		line[80], rfile[80];
static MPI_Status 	status;
static FILE		*fh, *fhlog;
static double 		l, dx, offset;

void init_io(ctrl_ptr ctrl)
{
  MPI_Comm_size(COMM, &np);
  MPI_Comm_rank(COMM, &myid);

  l 	= ctrl->l;
  n     = ctrl->n;
  dx 	= l/n; offset  = ctrl->offset;
  strcpy(rfile,ctrl->rname);
  MPI_Barrier(COMM);  

  if (myid == 0) {
   sprintf(line,"%s.log",ctrl->runname);
   fh = fopen(rfile,"rb"); 
   if (fh == NULL) {
     fhlog = fopen(line,"a");
     fprintf(fhlog,"Restart file is missing\nStarting new run\n");
     fclose(fhlog);
     ctrl->rs_flag = 0;
   } else {
     fhlog = fopen(line,"a");
     fprintf(fhlog,"Restart file found.\nRestarting from %s\n",ctrl->rname);
     fclose(fhlog);
     fclose(fh);
     ctrl->rs_flag = 1;     
   }
  }
  MPI_Barrier(COMM);  
  MPI_Bcast(ctrl, sizeof(ctrl_str), MPI_BYTE, 0, COMM);
  MPI_Barrier(COMM);  

}

void save_data(work_ptr in, char* outfile)
{
  int numel;
  MPI_Comm_size(COMM, &np);
  MPI_Comm_rank(COMM, &myid);	  

  MPI_Barrier(COMM);
  if (myid == 0) fh = fopen(outfile,"wb");
  else {
    MPI_Recv(&flag, 1, INT, myid-1, myid, COMM, &status);  
    fh = fopen(outfile, "ab");
  }
  numel = fwrite(in->wrk,sizeof(fftw_complex),in->alloc_local,fh);
  fclose(fh); 
  if (myid != np-1)  MPI_Send(&flag, 1, INT, myid+1, myid+1, COMM);
  MPI_Barrier(COMM);
}

void save_data2(work_ptr in, char* outfile,fftw_complex* ptr)
{
  int numel;
  MPI_Comm_size(COMM, &np);
  MPI_Comm_rank(COMM, &myid);	  

  MPI_Barrier(COMM);
  if (myid == 0) fh = fopen(outfile,"w");
  else {
    MPI_Recv(&flag, 1, INT, myid-1, myid, COMM, &status);  
    fh = fopen(outfile, "a");
  }
  for (i = 0; i < in->l_n0; i++) for ( j = 0; j < n; j++) for (k = 0; k < n; k++) {
  	fprintf(fh, "%e\n", cabs(ptr[k+n*(j + n*i)]) );
  } 
  //numel = fwrite(ptr,sizeof(fftw_complex),(in->l_n0)*n*n,fh);
  fclose(fh); 
  if (myid != np-1)  MPI_Send(&flag, 1, INT, myid+1, myid+1, COMM);
  MPI_Barrier(COMM);
}

void load_data(work_ptr in, char* infile)
{
  MPI_Comm_size(COMM, &np); int numel;
  MPI_Comm_rank(COMM, &myid);
  buffsize = (in->alloc_local)*sizeof(fftw_complex);
 
  MPI_Barrier(COMM);
  if (myid != 0) MPI_Recv(&flag, 1, INT, myid-1, myid, COMM, &status);
  fh = fopen(infile, "rb"); 
  fseek(fh, myid*buffsize, SEEK_SET);
  numel = fread(in->wrk, sizeof(fftw_complex), in->alloc_local, fh);
  fclose(fh);
  if (myid != np-1) MPI_Send(&flag, 1, INT, myid+1, myid+1, COMM);
  MPI_Barrier(COMM);
}
void save_text(work_ptr in, char* infile)
{
  int mid, l_s;
  double ab, bc, cd, cd2, kz, ky, kx;
  char file2[80];
  MPI_Comm_size(COMM, &np);
  MPI_Comm_rank(COMM, &myid);
  
  mid = np/2;
  MPI_Barrier(COMM);  
  convolution(in);
  if (myid == mid) {
    fh = fopen(infile, "w");  l_s = in->l_0_start;
    fprintf(fh, "#We are looking at process %d here\n", mid);    
    fprintf(fh, "#Text data for easier debug\n#1.y\t2.z\t");
    fprintf(fh, "3.|psi|\t\n\n");
    for (j = 0; j < n; j++){
      for (k = 0; k < n; k++){	  
          ab = cabs(in->wrk[k+n*(j+i)]);
	  bc = atan2(cimag(in->wrk[k+n*(j+i)]),creal(in->wrk[k+n*(j+i)]));
          //cd = cabs(in->grv[k + n*(j + i)]);
          //kz = k; ky = j; kx = i + myid*(in->l_n0);
          //if (kz >= n/2) kz = kz - n;
	  //if (ky >= n/2) ky = ky - n;
          //if (kx >= n/2) kx = kx - n;
          fprintf(fh, "%e\t%e\t%e\t%e\n",-l/2+(j+0.5)*dx,-l/2+(k+0.5)*dx,ab,bc);
      }
      fprintf(fh,"\n");
    }
    fclose(fh);
  }
  MPI_Barrier(COMM);
  /*if (myid == 0) {
    sprintf(file2,"%s.fft",infile);
    //printf("%s\n",file2);
    fhlog = fopen(file2, "w");
    fprintf(fhlog, "#We are looking at process 0 here.\n#File contains debug data in Fourier space\n");
    fprintf(fhlog, "#1.ky\t2.kz\t3.grv\t4.grv2 (Explicit expression for 1/r in F-space)\n\n");
    for (j = 0; j < n; j++){
      for (k = 0; k < n; k++){
          cd  = cabs(in->grv[k+n*j]);
	  cd2 = cabs(in->grv2[k+n*j]);
          kz = k; ky = j;
          if (k >= n/2) kz = k - n;		
          if (j >= n/2) ky = j - n;
          fprintf(fhlog, "%e\t%e\t%e\t%e\n",ky*(2*PI/l), kz*(2*PI/l), cd, cd2);
       }
       fprintf(fhlog,"\n");
    }
    fclose(fhlog);
  }*/

}
